<template>
  <div class="upload-page">
    <div class="particles-background" id="particles-js"></div>
    <div class="upload-container">
      <h2 class="section-title">
        <span class="title-icon">📦</span>
        <span class="title-text">项目上链</span>
      </h2>

      <form @submit.prevent="uploadProject" class="upload-form">
        <div class="form-group" v-for="(field, key) in form" :key="key">
          <!-- <label class="form-group" :for="key">{{ field.label }}</label> -->
          <div class="input-wrapper">
            <input
              v-model="field.value"
              :id="key"
              type="text"
              :placeholder="field.placeholder"
              required
              class="form-input"
            />
            <div class="input-highlight"></div>
          </div>
        </div>

        <button type="submit" class="submit-button">
          <i class="fas fa-cloud-upload-alt"></i>
          <span>提交上链</span>
          <div class="button-hover-effect"></div>
        </button>
      </form>

      <div v-if="status" class="status-message" :class="{'success': status.includes('成功'), 'error': status.includes('失败')}">
        <i :class="status.includes('成功') ? 'fas fa-check-circle' : 'fas fa-exclamation-circle'"></i>
        <span>{{ status }}</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import Web3 from 'web3'
import DonationPlatformABI from 'E:/Vue3/vite-project/artifacts/contracts/DonationPlatform.sol/DonationPlatform.json'

const status = ref('')

const form = {
  name: {
    label: '项目名称',
    placeholder: '如：乡村教育计划',
    value: ''
  },
  shortDescription: {
    label: '简短描述',
    placeholder: '为偏远地区儿童提供教育资源',
    value: ''
  },
  description: {
    label: '详细描述',
    placeholder: '例如项目目标、执行计划、影响力等',
    value: ''
  },
  image: {
    label: '图片链接',
    placeholder: 'https://example.com/image.png',
    value: ''
  },
  goal: {
    label: '目标金额（ETH）',
    placeholder: '如：50',
    value: ''
  },
  tag: {
    label: '分类标签',
    placeholder: '如：1（教育）',
    value: ''
  },
  walletAddress: {
    label: '接收捐赠的钱包地址',
    placeholder: '0x...',
    value: ''
  }
}

onMounted(() => {
  if (typeof window !== 'undefined') {
    import('particles.js').then(({ default: particlesJS }) => {
      particlesJS('particles-js', {
        particles: {
          number: { value: 80, density: { enable: true, value_area: 800 } },
          color: { value: "#4a8bfc" },
          shape: { type: "circle" },
          opacity: { value: 0.5, random: true },
          size: { value: 3, random: true },
          line_linked: { 
            enable: true, 
            distance: 150, 
            color: "#4a8bfc", 
            opacity: 0.4, 
            width: 1 
          },
          move: { 
            enable: true, 
            speed: 2, 
            direction: "none", 
            random: true, 
            straight: false, 
            out_mode: "out" 
          }
        },
        interactivity: {
          detect_on: "canvas",
          events: {
            onhover: { enable: true, mode: "grab" },
            onclick: { enable: true, mode: "push" }
          }
        }
      });
    });
  }
});

const uploadProject = async () => {
  try {
    if (!window.ethereum) return alert('请先安装 MetaMask')

    const web3 = new Web3(window.ethereum)
    await window.ethereum.request({ method: 'eth_requestAccounts' })

    const accounts = await web3.eth.getAccounts()
    const contractAddress = '0x1f936366c9b21fC9343BC2DC710031aa01be072E'
    const contract = new web3.eth.Contract(DonationPlatformABI.abi, contractAddress)

    const {
      name,
      shortDescription,
      description,
      image,
      goal,
      tag,
      walletAddress
    } = Object.fromEntries(Object.entries(form).map(([key, val]) => [key, val.value]))

    // 输入校验
    if (!name.trim()) return alert('项目名称不能为空')
    if (!goal || isNaN(Number(goal))) return alert('目标金额必须是数字')
    if (!tag || isNaN(Number(tag))) return alert('分类标签必须是数字')
    if (!walletAddress || !web3.utils.isAddress(walletAddress)) return alert('钱包地址不合法')

    // 转换
    const goalInWei = web3.utils.toWei(goal.toString(), 'ether')
    const tagNumber = Number(tag)

    await contract.methods
      .createProject(
        name,
        shortDescription,
        description,
        image,
        goalInWei,
        0,
        0,
        0,
        tagNumber,
        0,
        walletAddress
      )
      .send({ from: accounts[0] })

    status.value = '✅ 项目已成功上链！'
  } catch (error) {
    console.error('❌ 上链失败:', error)
    status.value = `❌ 上链失败: ${error.message}`
  }
}
</script>

<style scoped>
/* 基础变量 */
:root {
  --primary-color: #4a8bfc;
  --accent-color: #6c5ce7;
  --dark-color: #2d3436;
  --light-color: #f5f6fa;
  --success-color: #00b894;
  --error-color: #d63031;
  --border-radius: 12px;
  --box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  --transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
  --glow-effect: 0 0 15px rgba(74, 139, 252, 0.5);
}

/* 页面布局 */
.upload-page {
  position: relative;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #0f2027, #203a43, #2c5364);
  overflow: hidden;
  padding: 20px;
  font-family: 'Poppins', sans-serif;
}

.particles-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
}

.upload-container {
  position: relative;
  width: 100%;
  max-width: 800px;
  background: rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(10px);
  border-radius: var(--border-radius);
  padding: 40px;
  box-shadow: var(--box-shadow), 0 0 30px rgba(74, 139, 252, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.1);
  z-index: 1;
  transform: translateY(20px);
  opacity: 0;
  animation: slideUp 0.8s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}

@keyframes slideUp {
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

/* 标题样式 */
.section-title {
  font-size: 2rem;
  margin-bottom: 40px;
  color: var(--light-color);
  text-align: center;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.title-icon {
  font-size: 2.5rem;
  margin-bottom: 15px;
  display: inline-block;
  animation: float 3s ease-in-out infinite;
}

.title-text {
  position: relative;
  padding-bottom: 10px;
}

.title-text::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 80px;
  height: 3px;
  background: linear-gradient(90deg, var(--primary-color), var(--accent-color));
  border-radius: 3px;
  box-shadow: var(--glow-effect);
}

@keyframes float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-10px); }
}

/* 表单样式 */
.upload-form {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 25px;
}

.form-group {
  margin-bottom: 25px;
  position: relative;
  animation: fadeIn 0.6s ease-out forwards;
  animation-delay: calc(var(--order) * 0.1s);
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

.form-group:nth-child(1) { --order: 1; }
.form-group:nth-child(2) { --order: 2; }
.form-group:nth-child(3) { --order: 3; }
.form-group:nth-child(4) { --order: 4; }
.form-group:nth-child(5) { --order: 5; }
.form-group:nth-child(6) { --order: 6; }
.form-group:nth-child(7) { --order: 7; }

.form-group:nth-last-child(3),
.form-group:nth-last-child(2),
.form-group:last-child {
  grid-column: span 2;
}

label {
  display: block;
  font-weight: 500;
  margin-bottom: 12px;
  color: rgba(255, 255, 255, 0.9);
  font-size: 0.95rem;
  transition: var(--transition);
  position: relative;
  padding-left: 5px;
}

.input-wrapper {
  position: relative;
}

.form-input {
  width: 100%;
  padding: 14px 15px;
  border: none;
  border-radius: var(--border-radius);
  font-size: 0.95rem;
  transition: var(--transition);
  background: rgba(255, 255, 255, 0.1);
  color: white;
  box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.1);
  position: relative;
  z-index: 1;
}

.form-input::placeholder {
  color: rgba(255, 255, 255, 0.4);
}

.form-input:focus {
  outline: none;
  background: rgba(255, 255, 255, 0.15);
  box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.2), 0 0 15px rgba(74, 139, 252, 0.3);
}

.input-highlight {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 0;
  height: 2px;
  background: linear-gradient(90deg, var(--primary-color), var(--accent-color));
  transition: var(--transition);
  z-index: 2;
}

.form-input:focus ~ .input-highlight {
  width: 100%;
}

/* 提交按钮 */
.submit-button {
  grid-column: span 2;
  padding: 16px;
  background: linear-gradient(135deg, var(--primary-color), var(--accent-color));
  color: white;
  border: none;
  border-radius: var(--border-radius);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: var(--transition);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-top: 10px;
  position: relative;
  overflow: hidden;
  z-index: 1;
  box-shadow: 0 5px 15px rgba(74, 139, 252, 0.4);
}

.submit-button:hover {
  transform: translateY(-3px);
  box-shadow: 0 8px 25px rgba(74, 139, 252, 0.6);
}

.submit-button:active {
  transform: translateY(0);
}

.button-hover-effect {
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: 0.5s;
  z-index: -1;
}

.submit-button:hover .button-hover-effect {
  left: 100%;
}

/* 状态消息 */
.status-message {
  margin-top: 25px;
  padding: 15px;
  border-radius: var(--border-radius);
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 500;
  animation: fadeIn 0.5s ease-out;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(5px);
}

.status-message.success {
  background: rgba(0, 184, 148, 0.1);
  color: var(--success-color);
  border-left: 4px solid var(--success-color);
}

.status-message.error {
  background: rgba(214, 48, 49, 0.1);
  color: var(--error-color);
  border-left: 4px solid var(--error-color);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .upload-form {
    grid-template-columns: 1fr;
  }
  
  .form-group:nth-last-child(3),
  .form-group:nth-last-child(2),
  .form-group:last-child {
    grid-column: span 1;
  }
  
  .submit-button {
    grid-column: span 1;
  }
  
  .upload-container {
    padding: 25px;
  }
  
  .section-title {
    font-size: 1.8rem;
  }
}

/* 3D 卡片效果 */
.upload-container::before {
  content: '';
  position: absolute;
  top: -2px;
  left: -2px;
  right: -2px;
  bottom: -2px;
  background: linear-gradient(45deg, #4a8bfc, #6c5ce7, #4a8bfc);
  z-index: -1;
  border-radius: calc(var(--border-radius) + 2px);
  opacity: 0;
  transition: var(--transition);
}

.upload-container:hover::before {
  opacity: 0.1;
}

/* 霓虹效果 */
@keyframes neon {
  from {
    text-shadow: 0 0 5px #fff, 0 0 10px #fff, 0 0 15px var(--primary-color), 0 0 20px var(--primary-color);
  }
  to {
    text-shadow: 0 0 10px #fff, 0 0 15px #fff, 0 0 20px var(--primary-color), 0 0 25px var(--primary-color);
  }
}

.status-message.success i,
.status-message.error i {
  animation: neon 1.5s ease-in-out infinite alternate;
}
</style>